home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / misc / joy2key_1_0.lha / Joy2Key.c < prev    next >
C/C++ Source or Header  |  1992-09-06  |  6KB  |  210 lines

  1. /**************************************************************************
  2. *  NAME: Joy2Key                                               _          *
  3. *  FUNCTION: Translate joystick movement into key press     _ //   _      *
  4. *  PROGRAMMER: Piero Filippin (omega@sabrina.unipd.dei.it)  \X/ _ //   _  *
  5. *                                                               \X/ _ //  *
  6. *  This program is Public Domain (donation pleased)                 \X/   *
  7. *                                                                         *
  8. **************************************************************************/
  9.  
  10. #define IFFIRE 0x4C,0        /*cursor up         */
  11. #define IFLEFT 0x4F,0        /*cursor left       */
  12. #define IFRIGHT 0x4E,0       /*cursor right      */
  13. #define IFDOWN 0x4D,0        /*cursor down       */
  14. #define IFUP 0x4C,0          /*cursor up         */
  15. #define CODETOQUIT 0x10      /* q +              */
  16. #define QUALIFIERTOQUIT 0x19|IEQUALIFIER_RELATIVEMOUSE /* LALT+LSHIFT+CTRL */
  17. #define TIMEDELAY 5
  18.  
  19. #include <exec/types.h>
  20. #include <clib/alib_protos.h>
  21. #include <proto/exec.h>
  22. #include <proto/dos.h>
  23. #include <string.h>
  24. #include <exec/interrupts.h>
  25. #include <devices/input.h>
  26. #include <hardware/custom.h>
  27. #include <hardware/cia.h>
  28.  
  29. #define CIAAPRA 0xBFE001
  30. #define FIRE   1
  31. #define RIGHT  2
  32. #define LEFT   4
  33. #define DOWN   8
  34. #define UP    16
  35. #define PORT1 1
  36. #define PORT2 2
  37.  
  38. /* Funzioni */
  39. void main(void);
  40. void RemoveHandler(void);
  41. void InstallHandler(void);
  42. void RawInsert(short code,short qualifier); /* insert RAWKEY event */
  43. UBYTE Joystick(UBYTE  port ); /* returns joy status */
  44.  
  45. struct handlerdata {
  46.     struct Task *thistask;
  47.     short sigbit;
  48.     long code;
  49.     long qualifier;
  50. };
  51.  
  52. struct MsgPort *inputDevPort;
  53. struct Interrupt handlerStuff;
  54. struct IOStdReq *inputRequestBlock;
  55. struct handlerdata data;
  56. struct Custom *custom =(struct Custom *)0xDFF000; /* this in no sure, but don't need amiga lib */
  57. struct CIA *cia = (struct CIA *) CIAAPRA;
  58.  
  59. /* Declarations for CBACK */
  60. extern BPTR _Backstdout;         /* standard output when run in background */
  61. long _BackGroundIO = TRUE;       /* Flag to tell it we want to do I/O      */
  62. long _stack = 1024;              /* Amount of stack space our task needs   */
  63. char *_procname = "Joy2Key";     /* The name of the task to create         */
  64. long _priority = 0;              /* The priority to run us at              */
  65.  
  66. void Print(char *stringa); /* We are running in background, so cannot use printf()*/
  67.  
  68. void Print(char *stringa)
  69. {
  70.     Write(_Backstdout,stringa,strlen (stringa));
  71. }
  72.  
  73. void main(void)
  74. {
  75.   UBYTE value = 0;
  76.     Print("\n\nJoy2Key v1.0 - By Piero Filippin\n\n");
  77. #ifndef DEBUG
  78.     Close(_Backstdout);
  79. #endif
  80.     data.code=CODETOQUIT;
  81.     data.qualifier=QUALIFIERTOQUIT;
  82.     data.sigbit = AllocSignal(-1);
  83.     data.thistask=FindTask(NULL);
  84.     InstallHandler();
  85.     while (!((data.thistask->tc_SigRecvd) & (1 << data.sigbit))) { /* we cannot use Wait() */
  86.     value = Joystick( PORT2 );
  87.     if( value & FIRE ) {
  88. #ifdef DEBUG
  89.             Print("FIRE\n");
  90. #endif
  91.       RawInsert(IFFIRE);
  92.     }
  93.     if( value & RIGHT ) {
  94. #ifdef DEBUG
  95.             Print("RIGHT\n");
  96. #endif
  97.       RawInsert(IFRIGHT);
  98.     }
  99.     if( value & LEFT ) {
  100. #ifdef DEBUG
  101.             Print("LEFT\n");
  102. #endif
  103.       RawInsert(IFLEFT);
  104.     }
  105.     if( value & DOWN ) {
  106. #ifdef DEBUG
  107.             Print("DOWN\n");
  108. #endif
  109.       RawInsert(IFDOWN);
  110.     }
  111.     if( value & UP ) {
  112. #ifdef DEBUG
  113.             Print("UP\n");
  114. #endif
  115.       RawInsert(IFUP);
  116.     }
  117.         Delay(TIMEDELAY);
  118.   }
  119.     RemoveHandler();
  120. #ifdef DEBUG
  121.     Close(_Backstdout);
  122. #endif
  123.     FreeSignal(data.sigbit);
  124.     Exit(0);
  125. }
  126.  
  127. struct InputEvent *myhandler(struct InputEvent *event,struct handlerdata *data)
  128. {
  129.     register struct InputEvent *ep, *lastevent;
  130.     for (ep = event, lastevent = NULL; ep != NULL; ep = ep->ie_NextEvent) {
  131.         if ((ep->ie_Class == IECLASS_RAWKEY) && (ep->ie_Code  == data->code) && (ep->ie_Qualifier == data->qualifier)) {
  132.             if (lastevent == NULL) event = ep->ie_NextEvent;
  133.             else lastevent->ie_NextEvent = ep->ie_NextEvent;
  134.             lastevent = ep;
  135.             Signal(data->thistask,1 << data->sigbit);
  136.         }
  137.     }
  138.     return(event);
  139. }
  140.  
  141. void RemoveHandler(void)
  142. {
  143.     if (inputRequestBlock) {
  144.         if (inputRequestBlock->io_Device) {
  145.             inputRequestBlock->io_Command = IND_REMHANDLER;  /* Remove handler */
  146.             inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  147.             DoIO((struct IORequest *)inputRequestBlock);
  148.             CloseDevice((struct IORequest *)inputRequestBlock);
  149.         }
  150.         DeleteStdIO(inputRequestBlock);
  151.     }
  152.     if (inputDevPort) {
  153.         DeletePort(inputDevPort);
  154.     }
  155.     return;
  156. }
  157.  
  158. void InstallHandler(void)
  159. {
  160.     inputRequestBlock = NULL;
  161.     if (!(inputDevPort = CreatePort(0L, 0L))) {
  162.         RemoveHandler();
  163.     }
  164.     if (!(inputRequestBlock = CreateStdIO(inputDevPort))) {
  165.         RemoveHandler();
  166.     }
  167.     if (OpenDevice("input.device", 0L,(struct IORequest *)inputRequestBlock, 0L)) {
  168.         RemoveHandler();
  169.     }
  170.     handlerStuff.is_Data = (APTR)&data;         /* Set up for installation of */
  171.     handlerStuff.is_Code = myhandler;          /* myhandler.                 */
  172.     handlerStuff.is_Node.ln_Pri = 51;         /* Ahead of intuition, please */
  173.     inputRequestBlock->io_Command = IND_ADDHANDLER;
  174.     inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  175.     DoIO((struct IORequest *)inputRequestBlock);   /* Add me. */
  176.     return;
  177. }
  178.  
  179. void RawInsert(short code,short qualifier) {
  180.     /* Set up an input request */
  181.     struct InputEvent MyNewEvent;
  182.     inputRequestBlock->io_Command = IND_WRITEEVENT;
  183.     inputRequestBlock->io_Flags   = 0L;
  184.     inputRequestBlock->io_Length  = (long)sizeof(struct InputEvent);
  185.     inputRequestBlock->io_Data    = (APTR)&MyNewEvent;
  186.     MyNewEvent.ie_Class = IECLASS_RAWKEY;
  187.     MyNewEvent.ie_Code = code;
  188.     MyNewEvent.ie_Qualifier = qualifier;
  189.     DoIO((struct IORequest *)inputRequestBlock);
  190. }
  191.  
  192. UBYTE Joystick(UBYTE  port )
  193. {
  194.     UBYTE data = 0;
  195.     UWORD joy;
  196.     if( port == PORT1 ) {
  197.         joy = custom->joy0dat;
  198.         data += !( cia->ciapra & 0x0040 ) ? FIRE : 0;
  199.     }
  200.     else {
  201.         joy = custom->joy1dat;
  202.         data += !( cia->ciapra & 0x0080 ) ? FIRE : 0;
  203.     }
  204.     data += joy & 0x0002 ? RIGHT : 0;
  205.     data += joy & 0x0200 ? LEFT : 0;
  206.     data += (joy >> 1 ^ joy) & 0x0001 ? DOWN : 0;
  207.     data += (joy >> 1 ^ joy) & 0x0100 ? UP : 0;
  208.     return( data );
  209. }
  210.